﻿using System;
using biz.ritter.javapi.io;
using biz.ritter.javapi.lang;
using biz.ritter.javapi.util;

namespace Nofs.Net.Fuse.Impl
{
    public class PathUtil
    {
        private static String _separatorChar = "" + File.separatorChar;

        public static LinkedList<String> SplitPath(String path)
        {
            LinkedList<String> segments = new LinkedList<String>();
            StringTokenizer parts = new StringTokenizer(path, _separatorChar);
            while (parts.hasMoreTokens())
            {
                segments.add(parts.nextToken());
            }
            return segments;
        }

        public static String PathAfter(String path, int level)
        {
            StringBuffer newPath = new StringBuffer();
            List<String> segments = SplitPath(path);
            for (int i = level + 1; i < segments.size(); i++)
            {
                newPath.append(_separatorChar);
                newPath.append(segments.get(i));
            }
            return newPath.toString();
        }

        public static String GetParentName(String path)
        {
            //String[] parts = path.split("\\/");
            String[] parts = path.Split(("\\" + _separatorChar).ToCharArray());
            String parent = "";
            if (parts.Length == 2)
            {
                parent = _separatorChar;
            }
            else
            {
                for (int i = 1; i < parts.Length - 1; i++)
                {
                    parent += _separatorChar + parts[i];
                }
            }
            return parent;
        }

        public static String GetChildName(String path)
        {
            String[] parts = path.Split(("\\" + _separatorChar).ToCharArray());
            return parts.Length == 0 ? _separatorChar : parts[parts.Length - 1];
        }
    }

}
